<template>
  <div class="goodDetail_container">
    <!-- 添加购物车小球 -->
    <transition @beforeEnter="beforeEnter" @enter="enter" @afterEnter="afterEnter">
    <div class="ball" v-show="ballFlag" ref="ball"></div>
    </transition>
    <div class="mui-card">
      <div class="mui-card-content">
        <!-- 轮播图组件 -->
        <swiper :lunboList="lunboList" :isFull="false"></swiper>
      </div>
    </div>
    <div class="mui-card">
      <div class="mui-card-header">{{goodInfo.title}}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价：
            <span class="market">
              ￥{{goodInfo.market_price}}
            </span>
            &nbsp;&nbsp;销售价：
            <span class="sell_price">￥{{goodInfo.sell_price}}</span>
          </p>
          <p>
            购买数量：
            <!-- 将父组件方法传递给子组件  记住：传递方法使用@、属性用v-bind（ ：）-->
            <number_box @getCount="getSelectedCount" :max="goodInfo.stock_quantity"></number_box>
            <!-- 将数字子组件选中的商品数量传递给父组件：父组件申明方法，子组件调用并传值 -->
          </p>
          <div>
            <mt-button type="primary" size="small">立即购买</mt-button>
            <mt-button type="danger" size="small" @click="addShopcar">加入购物车</mt-button>
          </div>
        </div>
      </div>
    </div>
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号：{{goodInfo.goods_no}}</p>
          <p>库存情况：{{goodInfo.stock_quantity}}件</p>
          <p>上架时间：{{goodInfo.add_time |dateFormat("YYYY-MM-DD HH:mm:ss")}}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" plain @click="goodsDesc">图文介绍</mt-button>
        <mt-button type="danger" size="large" plain @click="goodsComment">商品评价</mt-button>
      </div>
    </div>
  </div>
</template>
<script>
import swiper from "../subcomponents/swiper.vue";
import number_box from "../subcomponents/numberBox.vue";
export default {
  data() {
    return {
      id: this.$route.params.id,
      lunboList: [], //存放轮播图数据
      goodInfo: [],//商品详情
      ballFlag:false,//小球显示标识
      selectedCount:1//用户选中的商品数量，默认为1
    };
  },
  created() {
    this.getLunboList();
    this.getGoodInfo();
  },
  methods: {
    getLunboList() {
      this.$http.get("api/getthumimages/" + this.id).then(result => {
        if (result.body.status === 0) {
          //   result.body.message.forEach(element => {
          //       element.img=element.src;
          //   });
          this.lunboList = result.body.message;
        }
      });
    },
    getGoodInfo() {
      this.$http.get("api/goods/getinfo/" + this.id).then(result => {
        if (result.body.status === 0) {
          this.goodInfo = result.body.message[0];
        }
      });
    },
    //编程式导航跳转页面
    goodsDesc() {
        this.$router.push({name:"goodsDesc",params:{id:this.id}});
    },
    goodsComment(){
        this.$router.push({name:"goodsComment",params:{id:this.id}});
    },
    addShopcar(){
      //点击加入购物车
      this.ballFlag=!this.ballFlag;
      const goodinfo ={
        id:this.id,
        count:this.selectedCount,
        price:this.goodInfo.sell_price,
        selected:true
      };
      this.$store.commit("addToShopcar",goodinfo)
    },
    beforeEnter(el){
      el.style.transform="translate(0,0)";
    },
    enter(el,done){
      el.offsetWidth;
      //这里解决动画适配不同分辨率的问题
      //获取小球的left，right，bottom，top值
      const ballPositions=this.$refs.ball.getBoundingClientRect();
      //获取购物车徽标的四个位置坐标
      const badgePositions=document.getElementById("badge").getBoundingClientRect();
      //计算徽标和小球的x、y差值
      const xDistance=badgePositions.left-ballPositions.left;
      const yDistance=badgePositions.top-ballPositions.top;
      el.style.transform=`translate(${xDistance}px,${yDistance}px)`;
      // 贝塞尔曲线动画效果：cubic-bezier(.17,.67,.83,.67)
      el.style.transition="all 0.5s cubic-bezier(.17,.67,.83,.67)";
      done();
    },
    afterEnter(el){
      this.ballFlag=!this.ballFlag;
    },
    getSelectedCount(count){
      this.selectedCount=count;
      // 用来接收数组子组件传递的商品数量
      // console.log("父组件接收到的商品数量为："+ this.selectedCount);
    }
  },
  components: {
    swiper,
    number_box
  }
};
</script>
<style lang="scss" scoped>
.goodDetail_container {
  background-color: #ddd;
  overflow: hidden;
  .ball{
    position: absolute;
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background-color: #ff0000;
    z-index: 99;
    top: 427px;
    left: 150px;
     //用来调试动画translate的x，y数值 
    //  transform: translate(73px,390px);
  }
  .market{
      text-decoration: line-through;
  }
  .sell_price {
    color: red;
    font-weight: bold;
  }
  .mui-card-footer {
    display: block;
    button {
      margin-bottom: 15px;
    }
  }
}
</style>
